Imports AxSftTreeLib70
Imports SftTreeLib70
Imports Softelvdm.OLEConvert

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
Friend WithEvents ItemContextMenu As System.Windows.Forms.ContextMenu
Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents menuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents menuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents menuItem4 As System.Windows.Forms.MenuItem
Public WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents ItemImageList As System.Windows.Forms.ImageList
Friend WithEvents PlusMinusImageList As System.Windows.Forms.ImageList
Friend WithEvents AxSftTree1 As AxSftTreeLib70.AxSftTree
Friend WithEvents UglyCheckBox As System.Windows.Forms.CheckBox
Friend WithEvents PlusMinusCheckBox As System.Windows.Forms.CheckBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ButtonImageList As System.Windows.Forms.ImageList
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents SftHelperComponent1 As Softelvdm.OCXHelper.SftHelperComponent
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.ItemContextMenu = New System.Windows.Forms.ContextMenu()
Me.menuItem1 = New System.Windows.Forms.MenuItem()
Me.menuItem2 = New System.Windows.Forms.MenuItem()
Me.menuItem3 = New System.Windows.Forms.MenuItem()
Me.menuItem4 = New System.Windows.Forms.MenuItem()
Me.ItemImageList = New System.Windows.Forms.ImageList(Me.components)
Me.PlusMinusImageList = New System.Windows.Forms.ImageList(Me.components)
Me.AxSftTree1 = New AxSftTreeLib70.AxSftTree()
Me.Label5 = New System.Windows.Forms.Label()
Me.UglyCheckBox = New System.Windows.Forms.CheckBox()
Me.PlusMinusCheckBox = New System.Windows.Forms.CheckBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.ButtonImageList = New System.Windows.Forms.ImageList(Me.components)
Me.Label1 = New System.Windows.Forms.Label()
Me.SftHelperComponent1 = New Softelvdm.OCXHelper.SftHelperComponent(Me.components)
CType(Me.AxSftTree1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ItemContextMenu
'
Me.ItemContextMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuItem1, Me.menuItem2, Me.menuItem3, Me.menuItem4})
'
'menuItem1
'
Me.menuItem1.Index = 0
Me.menuItem1.Text = "Edit"
'
'menuItem2
'
Me.menuItem2.Index = 1
Me.menuItem2.Text = "Insert"
'
'menuItem3
'
Me.menuItem3.Index = 2
Me.menuItem3.Text = "Append"
'
'menuItem4
'
Me.menuItem4.Index = 3
Me.menuItem4.Text = "Delete"
'
'ItemImageList
'
Me.ItemImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
Me.ItemImageList.ImageSize = New System.Drawing.Size(16, 16)
Me.ItemImageList.ImageStream = CType(resources.GetObject("ItemImageList.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ItemImageList.TransparentColor = System.Drawing.Color.Yellow
'
'PlusMinusImageList
'
Me.PlusMinusImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
Me.PlusMinusImageList.ImageSize = New System.Drawing.Size(14, 14)
Me.PlusMinusImageList.ImageStream = CType(resources.GetObject("PlusMinusImageList.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.PlusMinusImageList.TransparentColor = System.Drawing.Color.White
'
'AxSftTree1
'
Me.AxSftTree1.Location = New System.Drawing.Point(8, 48)
Me.AxSftTree1.Name = "AxSftTree1"
Me.AxSftTree1.OcxState = CType(resources.GetObject("AxSftTree1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxSftTree1.Size = New System.Drawing.Size(358, 372)
Me.AxSftTree1.TabIndex = 21
'
'Label5
'
Me.Label5.BackColor = System.Drawing.SystemColors.Control
Me.Label5.Cursor = System.Windows.Forms.Cursors.Default
Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label5.Location = New System.Drawing.Point(376, 168)
Me.Label5.Name = "Label5"
Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label5.Size = New System.Drawing.Size(88, 201)
Me.Label5.TabIndex = 20
Me.Label5.Text = "Click on the SftTree/OCX control and hit F1 to access online help for SftTree/OCX" & _
" 6.0"
Me.Label5.Visible = False
'
'UglyCheckBox
'
Me.UglyCheckBox.Location = New System.Drawing.Point(208, 424)
Me.UglyCheckBox.Name = "UglyCheckBox"
Me.UglyCheckBox.Size = New System.Drawing.Size(189, 23)
Me.UglyCheckBox.TabIndex = 19
Me.UglyCheckBox.Text = "Use ""Plain"" Buttons"
'
'PlusMinusCheckBox
'
Me.PlusMinusCheckBox.Location = New System.Drawing.Point(8, 424)
Me.PlusMinusCheckBox.Name = "PlusMinusCheckBox"
Me.PlusMinusCheckBox.Size = New System.Drawing.Size(190, 23)
Me.PlusMinusCheckBox.TabIndex = 18
Me.PlusMinusCheckBox.Text = "Show Plus/Minus Graphic"
'
'Button1
'
Me.Button1.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Button1.Location = New System.Drawing.Point(376, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(81, 30)
Me.Button1.TabIndex = 17
Me.Button1.Text = "Close"
'
'ButtonImageList
'
Me.ButtonImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
Me.ButtonImageList.ImageSize = New System.Drawing.Size(40, 9)
Me.ButtonImageList.ImageStream = CType(resources.GetObject("ButtonImageList.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ButtonImageList.TransparentColor = System.Drawing.Color.Yellow
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(366, 37)
Me.Label1.TabIndex = 16
Me.Label1.Text = "Click on header buttons to sort books or reorder the second and third column usin" & _
"g column drag && drop."
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.Button1
Me.ClientSize = New System.Drawing.Size(464, 446)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.Label1, Me.AxSftTree1, Me.Label5, Me.UglyCheckBox, Me.PlusMinusCheckBox})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.Name = "Form1"
Me.Text = "Softel vdm, Inc. - BookTable Sample"
CType(Me.AxSftTree1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

    End Sub

#End Region
    '--locate-marker--

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AxSftTree1.BulkUpdate = True

        ' set default item graphic.  This can also be done at design time
        AxSftTree1.Items.ItemImageExpanded.SetImageListH(ItemImageList.Handle.ToInt32(), 0)
        AxSftTree1.Items.ItemImageExpandable.SetImageListH(ItemImageList.Handle.ToInt32(), 1)
        AxSftTree1.Items.ItemImageLeaf.SetImageListH(ItemImageList.Handle.ToInt32(), 2)

        ' set column header pictures
        AxSftTree1.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortAsc
        AxSftTree1.get_Header(1).Image.Clear()
        AxSftTree1.get_Header(2).Image.Clear()

        Dim R As Random
        R = New Random()

        ' set the cell font for books
        Dim fnt As Font
        fnt = New Font(AxSftTree1.Font, FontStyle.Bold)
        Dim pFont As stdole.IFontDisp
        pFont = OLECvt.ToIFontDisp(fnt)

        Dim bk As Integer
        For bk = 1 To 4
            ' add a book
            Dim bookIndex As Integer
            bookIndex = AxSftTree1.Items.Add("Book " & bk)

            AxSftTree1.get_Cell(bookIndex, 1).Text = "Description for book " + bk.ToString()
            Dim size As Integer
            If bk = 1 Then
                size = R.Next(1, 99)
            Else
                size = R.Next(1, 999)
            End If
            AxSftTree1.get_Cell(bookIndex, 2).Text = size.ToString()
            AxSftTree1.get_Item(bookIndex).Data = size
            ' add chapters
            Dim ch As Integer
            For ch = 1 To 2
                Dim index As Integer
                index = AxSftTree1.Items.Add("Chapter " & ch)
                AxSftTree1.get_Item(index).Level = 1
                ' add sections
                Dim sect As Integer
                For sect = 1 To 2
                    index = AxSftTree1.Items.Add("Section " & sect)
                    AxSftTree1.get_Item(index).Level = 2
                Next
            Next
            ' after adding the book and all dependent items, we
            ' collapse the item, so it's up to the user to expand it
            AxSftTree1.get_Item(bookIndex).Collapse(False)
            ' set font
            AxSftTree1.get_Cell(bookIndex, 0).Font = pFont
        Next
        ' End of Mass-Update
        AxSftTree1.BulkUpdate = False
        ' Make columns optimal
        AxSftTree1.ColumnsObj.MakeOptimal()
        ' allow horizontal scrolling
        AxSftTree1.Items.RecalcHorizontalExtent()
    End Sub

    Private Sub PlusMinusCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusMinusCheckBox.CheckedChanged
        If PlusMinusCheckBox.Checked Then
            AxSftTree1.Items.PlusMinusImageExpandable.NETImageObject = PlusMinusImageList.Images(0)
            AxSftTree1.Items.PlusMinusImageExpanded.NETImageObject = PlusMinusImageList.Images(1)
        Else
            AxSftTree1.Items.PlusMinusImageExpandable.Clear()
            AxSftTree1.Items.PlusMinusImageExpanded.Clear()
        End If
    End Sub

    Private Sub UglyCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UglyCheckBox.CheckedChanged
        If UglyCheckBox.Checked Then
            AxSftTree1.ButtonPicture = ButtonImageList.Images(0)
        Else
            AxSftTree1.ButtonPicture = Nothing
        End If
    End Sub

    Private Sub SortHeader(ByVal ColNum As Integer)
        If AxSftTree1.get_Header(ColNum).Image.Appearance = SftPictureImageConstants.sftImageSortAsc Then
            ' Sort the data. Note that column 2 is sorted by Item.Data, which is
            ' an integer value (book size in pages)
            If ColNum = 2 Then
                AxSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeDscItemData)
            Else
                AxSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeDescending)
            End If
            AxSftTree1.get_Header(ColNum).Image.Appearance = SftPictureImageConstants.sftImageSortDesc
        Else
            AxSftTree1.get_Header(0).Image.Clear()
            AxSftTree1.get_Header(1).Image.Clear()
            AxSftTree1.get_Header(2).Image.Clear()
            ' Sort the data. Note that column 2 is sorted by Item.Data, which is
            ' an integer value (book size in pages)
            If ColNum = 2 Then
                AxSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeAscItemData)
            Else
                AxSftTree1.Items.SortDependents(-1, ColNum, SftTreeSortTypeConstants.sortSftTreeAscending)
            End If
            AxSftTree1.get_Header(ColNum).Image.Appearance = SftPictureImageConstants.sftImageSortAsc
        End If
    End Sub

    Private Sub AxSftTree1_ItemClick(ByVal sender As Object, ByVal e As AxSftTreeLib70._DSftTreeEvents_ItemClickEvent) Handles AxSftTree1.ItemClick
        Dim AreaType As SftTreeAreaTypeConstants
        AreaType = e.areaType
        Dim Button As SftTreeButtonConstants
        Button = e.button
        ' respond to click in column header with left mouse button
        If AreaType = SftTreeAreaTypeConstants.constSftTreeColumnHeader And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            SortHeader(e.colIndex)
        ElseIf AreaType = SftTreeAreaTypeConstants.constSftTreeExpandAll Then
            AxSftTree1.get_Item(e.itemIndex).Expand(False, True)
        End If
    End Sub

    Private Sub AxSftTree1_ItemDblClick(ByVal sender As Object, ByVal e As AxSftTreeLib70._DSftTreeEvents_ItemDblClickEvent) Handles AxSftTree1.ItemDblClick
        Dim AreaType As SftTreeAreaTypeConstants
        AreaType = e.areaType
        Dim Button As SftTreeButtonConstants
        Button = e.button
        ' Respond to click in column header with left mouse button
        If AreaType = SftTreeAreaTypeConstants.constSftTreeColumnRes And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            AxSftTree1.get_Column(e.colIndex).MakeOptimal()
            AxSftTree1.Items.RecalcHorizontalExtent()
        ElseIf AreaType = SftTreeAreaTypeConstants.constSftTreeColumnHeader And Button = SftTreeButtonConstants.constSftTreeLeftButton Then
            SortHeader(e.colIndex)
        End If
    End Sub

    Private Sub menuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem1.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem2.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem3.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub menuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItem4.Click
        MessageBox.Show("This sample doesn't implement any actions for the item menu.  Try the column headers instead.")
    End Sub

    Private Sub AxSftTree1_ContextMenuEvent(ByVal sender As Object, ByVal e As AxSftTreeLib70._DSftTreeEvents_ContextMenuEvent) Handles AxSftTree1.ContextMenuEvent
        Dim P As Point
        P = AxSftTree1.PointToScreen(New Point(e.x, e.y))

        ' Determine click context menu for header or item
        Dim L As Integer, T As Integer, H As Integer, W As Integer
        AxSftTree1.Headers.GetPositionPix(L, T, W, H)
        If e.x >= L And e.x < L + W And e.y >= T And e.y <= T + H Then
            HeaderMenu(PointToClient(P))
            Return
        End If

        ' determine item right-clicked
        Dim ItemIndex As Integer
        ItemIndex = AxSftTree1.Items.HitTestPix(e.x, e.y)
        If ItemIndex >= 0 And ItemIndex < AxSftTree1.Items.Count Then
            AxSftTree1.Items.Current = ItemIndex
            AxSftTree1.get_Item(ItemIndex).Selected = True
            AxSftTree1.CancelMode()
            ItemContextMenu.Show(Me, PointToClient(P))
        End If
    End Sub

    Private Sub menuHeader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim mItem As MenuItem
        mItem = sender
        Dim colIndex As Integer
        colIndex = mItem.Index
        If mItem.Checked Then
            AxSftTree1.get_Column(colIndex).WidthPix = 0
        Else
            AxSftTree1.get_Column(colIndex).MakeOptimal()
        End If
        AxSftTree1.Items.RecalcHorizontalExtent()
        mItem.Checked = Not mItem.Checked
    End Sub

    Private Sub menuHeaderShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        AxSftTree1.ColumnsObj.MakeOptimal()
    End Sub

    Private Sub HeaderMenu(ByVal cursor As Point)

        Dim Menu As System.Windows.Forms.ContextMenu
        Menu = New ContextMenu()
        Dim mItem As System.Windows.Forms.MenuItem

        Dim Count As Integer
        Count = 0
        Dim C As Integer
        For C = 0 To AxSftTree1.ColumnsObj.Count - 1
            mItem = New System.Windows.Forms.MenuItem()
            mItem.Text = AxSftTree1.get_Header(C).Text
            If AxSftTree1.get_Column(C).WidthPix > 0 Then
                mItem.Checked = True
                Count = Count + 1
            Else
                mItem.Checked = False
            End If
            AddHandler mItem.Click, AddressOf Me.menuHeader_Click
            Menu.MenuItems.Add(mItem)
        Next

        If Count <= 1 Then
            For C = 0 To AxSftTree1.ColumnsObj.Count - 1
                If Menu.MenuItems(C).Checked Then
                    Menu.MenuItems(C).Enabled = False
                Else
                    Menu.MenuItems(C).Enabled = True
                End If
            Next
        End If

        mItem = New System.Windows.Forms.MenuItem("-")
        Menu.MenuItems.Add(mItem)

        mItem = New System.Windows.Forms.MenuItem("&Show All")
        mItem.Enabled = (Count < AxSftTree1.ColumnsObj.Count)
        AddHandler mItem.Click, AddressOf Me.menuHeaderShowAll_Click
        Menu.MenuItems.Add(mItem)

        AxSftTree1.CancelMode()
        Menu.Show(Me, cursor)
    End Sub

End Class